package com.innoking.docsys.user.service.impl;

import com.innoking.docsys.user.service.GetPyToNplService;
import com.innoking.docsys.user.service.NlpService;
import com.innoking.docsys.utils.ResultObjVO;
import com.innoking.docsys.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: HelloServiceImpl
 * Function:  TODO
 * Date:      2019/8/28 11:21
 * author     yeruixaing
 * QQ:        3141684159
 * version    V1.0
 */
@Service
public class NlpServiceImpl implements NlpService {

    @Autowired
    private GetPyToNplService getPyToNplService;

    @Override
    public List getList() {
        List<String> list = new ArrayList<>();
        String filePath = "D:\\pythonexe\\javafile.txt";
//        String filePath = "/home/solrdic/javafile.txt";
        File file = new File(filePath);
        //判断文件存在并且是文件
        Boolean boo = file.exists() && file.isFile();
        if (boo) {
            BufferedReader bufferedReader = null;
            BufferedReader bufferedReader1 = null;
            try {
                //构造一个BufferedReader类来读取文件
                bufferedReader = new BufferedReader(new FileReader(file));
                String linetxt = bufferedReader.readLine();
                bufferedReader1 = new BufferedReader(new FileReader(linetxt));
                String linetxt1 = null;
                //按使用readLine方法，一次读一行
                while ((linetxt1 = bufferedReader1.readLine()) != null) {
                    list.add(linetxt1);
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return list;
    }

    @Override
    public ResultObjVO addOrUpdateNpl(String str) {
        if(StringUtils.isBlank(str)){
            return ResultUtil.paramNull("未获取参数");
        }
        String filePath = "D:\\pythonexe\\pathfile.txt";
//        String filePath = "/home/pythonexe/pathfile.txt";
        File file = new File(filePath);
        //判断文件存在并且是文件
        Boolean boo = file.exists() && file.isFile();
        try {
            if (boo) {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                String linetxt = bufferedReader.readLine();
//                FileWriter fwriter = new FileWriter(linetxt);
                BufferedWriter fwriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (linetxt,true),"UTF-8"));
                fwriter.write(str);
                fwriter.flush();
                fwriter.close();//一定要关闭文件
                getPyToNplService.getPyToNpl();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
        return ResultUtil.success();
    }
}
